package com.softtown.modules.workflow.service.impl;

import com.softtown.common.service.impl.BaseServiceImpl;
import com.softtown.modules.workflow.dao.SwfNextAprvDefDao;
import com.softtown.modules.workflow.dto.SwfNextAprvDefDTO;
import com.softtown.modules.workflow.entity.SwfNextAprvDefEntity;
import com.softtown.modules.workflow.entity.SwfTaskEntity;
import com.softtown.modules.organization.entity.CompOrgEmplPosnEntity;
import com.softtown.modules.organization.service.CompOrgEmplPosnService;
import com.softtown.modules.workflow.service.SwfNextAprvDefService;
import com.softtown.modules.workflow.service.SwfTaskService;
import com.softtown.modules.sys.entity.CompSysUserEmplEntity;
import com.softtown.modules.sys.service.CompSysUserEmplService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: Dingyayong
 * @Date: 2019/8/22 15:39
 * @Description:
 */
@Service
public class SwfNextAprvDefServiceImpl extends BaseServiceImpl<SwfNextAprvDefDao, SwfNextAprvDefEntity> implements SwfNextAprvDefService {

    @Resource
    private CompOrgEmplPosnService compOrgEmplPosnService;

    @Resource
    private CompSysUserEmplService compSysUserEmplService;

    @Resource
    private SwfTaskService swfTaskService;

    /**
     * 出差申请获取下一审批人默认值
     *
     * @param userId 用户id
     * @param compId 公司id
     * @return com.softtown.modules.entity.SwfNextAprvDefEntity
     * @Author Dingyayong
     * @Date 2020/4/13 17:56
     */
    @Override
    public SwfNextAprvDefEntity getNextAprvt(Long userId, Long compId) {
        SwfNextAprvDefEntity swfNextAprvDefEntity = baseDao.getNextAprvt(userId, compId);
        if (swfNextAprvDefEntity != null) {
            CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(compId, swfNextAprvDefEntity.getNextAprvUserId());
            List<CompOrgEmplPosnEntity> compOrgEmplPosnEntityList = compOrgEmplPosnService.findListPosnEntity(compId, compSysUserEmplEntity.getEmplId());
            List<Long> posnIdS = new ArrayList<>();
            if (compOrgEmplPosnEntityList.size() > 0) {
                for (CompOrgEmplPosnEntity compOrgEmplPosnEntity : compOrgEmplPosnEntityList) {
                    posnIdS.add(compOrgEmplPosnEntity.getPosnId());
                }
                if(posnIdS.size() > 0 && swfNextAprvDefEntity.getNextAprvPostId() != null){
                    if (posnIdS.contains(swfNextAprvDefEntity.getNextAprvPostId())) {
                        return swfNextAprvDefEntity;
                    } else {
                        swfNextAprvDefEntity.setNextAprvPostId(null);
                        swfNextAprvDefEntity.setNextAprvUserId(null);
                        swfNextAprvDefEntity.setNextAprvPostName("");
                        return swfNextAprvDefEntity;
                    }
                }else {
                    swfNextAprvDefEntity.setNextAprvPostId(null);
                    swfNextAprvDefEntity.setNextAprvPostName("");
                    return swfNextAprvDefEntity;
                }

            }
            else {
                swfNextAprvDefEntity.setNextAprvPostId(null);
                swfNextAprvDefEntity.setNextAprvPostName("");
                return swfNextAprvDefEntity;
            }

        } else {
            return null;
        }
    }

    /**
     * 差旅报销申请申请获取下一审批人默认值
     *
     * @param swfNextAprvDefDTO 下一审批默认值DTO
     * @return com.softtown.modules.entity.SwfNextAprvDefEntity
     * @Author Dingyayong
     * @Date 2020/4/13 17:56
     */
    @Override
    public SwfNextAprvDefEntity getExpenseNextAprvt(SwfNextAprvDefDTO swfNextAprvDefDTO) {
        SwfNextAprvDefEntity swfNextAprvDefEntity = baseDao.getExpenseNextAprvt(swfNextAprvDefDTO.getUserId(), swfNextAprvDefDTO.getCompId());
        if (swfNextAprvDefEntity != null) {
            CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(swfNextAprvDefDTO.getCompId(), swfNextAprvDefEntity.getNextAprvUserId());
            List<CompOrgEmplPosnEntity> compOrgEmplPosnEntityList = compOrgEmplPosnService.findListPosnEntity(swfNextAprvDefDTO.getCompId(), compSysUserEmplEntity.getEmplId());
            List<Long> posnIdS = new ArrayList<>();
            for (CompOrgEmplPosnEntity compOrgEmplPosnEntity : compOrgEmplPosnEntityList) {
                posnIdS.add(compOrgEmplPosnEntity.getPosnId());
            }
            if(posnIdS.size() > 0 && swfNextAprvDefEntity.getNextAprvPostId() != null) {
                if (posnIdS.contains(swfNextAprvDefEntity.getNextAprvPostId())) {
                    return swfNextAprvDefEntity;
                } else {
                    swfNextAprvDefEntity.setNextAprvPostId(null);
                    swfNextAprvDefEntity.setNextAprvUserId(null);
                    swfNextAprvDefEntity.setNextAprvPostName("");
                    return swfNextAprvDefEntity;
                }
            }else {
                swfNextAprvDefEntity.setNextAprvPostId(null);
                swfNextAprvDefEntity.setNextAprvPostName("");
                return swfNextAprvDefEntity;
            }
        } else {
            return null;
        }
    }

    /**
     * 获取下一审批人默认值
     *
     * @param swfNextAprvDefDTO 下一审批默认值DTO
     * @return com.softtown.modules.entity.SwfNextAprvDefEntity
     * @Author Dingyayong
     * @Date 2020/4/13 17:57
     */
    @Override
    public SwfNextAprvDefEntity getAllNextAprvt(SwfNextAprvDefDTO swfNextAprvDefDTO) {
        SwfNextAprvDefEntity swfNextAprvDefEntity = baseDao.getAllNextAprvt(swfNextAprvDefDTO.getUserId(), swfNextAprvDefDTO.getCompId(), swfNextAprvDefDTO.getDocTypeValue());
        if (swfNextAprvDefEntity != null) {
            CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(swfNextAprvDefDTO.getCompId(), swfNextAprvDefEntity.getNextAprvUserId());
            List<CompOrgEmplPosnEntity> compOrgEmplPosnEntityList = compOrgEmplPosnService.findListPosnEntity(swfNextAprvDefDTO.getCompId(), compSysUserEmplEntity.getEmplId());
            List<Long> posnIdS = new ArrayList<>();
            for (CompOrgEmplPosnEntity compOrgEmplPosnEntity : compOrgEmplPosnEntityList) {
                posnIdS.add(compOrgEmplPosnEntity.getPosnId());
            }
            if(posnIdS.size() > 0 && swfNextAprvDefEntity.getNextAprvPostId() != null) {
                if (posnIdS.contains(swfNextAprvDefEntity.getNextAprvPostId())) {
                    return swfNextAprvDefEntity;
                } else {
                    swfNextAprvDefEntity.setNextAprvPostId(null);
                    swfNextAprvDefEntity.setNextAprvUserId(null);
                    swfNextAprvDefEntity.setNextAprvPostName("");
                    return swfNextAprvDefEntity;
                }
            }else {
                swfNextAprvDefEntity.setNextAprvPostId(null);
                swfNextAprvDefEntity.setNextAprvPostName("");
                return swfNextAprvDefEntity;
            }
        } else {
            return null;
        }
    }

    /**
     * 差旅报销申请申请获取下一审批人默认值
     *
     * @param userId 用户id
     * @param compId 公司id
     * @return com.softtown.modules.entity.SwfNextAprvDefEntity
     * @Author Dingyayong
     * @Date 2020/4/13 17:58
     */
    @Override
    public SwfNextAprvDefEntity getExpenseNextAprvtUp(Long userId, Long compId) {
        SwfNextAprvDefEntity swfNextAprvDefEntity = baseDao.getExpenseNextAprvt(userId, compId);
        if (swfNextAprvDefEntity != null) {
            CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(compId, swfNextAprvDefEntity.getNextAprvUserId());
            List<CompOrgEmplPosnEntity> compOrgEmplPosnEntityList = compOrgEmplPosnService.findListPosnEntity(compId, compSysUserEmplEntity.getEmplId());
            List<Long> posnIdS = new ArrayList<>();
            for (CompOrgEmplPosnEntity compOrgEmplPosnEntity : compOrgEmplPosnEntityList) {
                posnIdS.add(compOrgEmplPosnEntity.getPosnId());
            }
            if(posnIdS.size() > 0 && swfNextAprvDefEntity.getNextAprvPostId() != null) {
                if (posnIdS.contains(swfNextAprvDefEntity.getNextAprvPostId())) {
                    return swfNextAprvDefEntity;
                } else {
                    swfNextAprvDefEntity.setNextAprvPostId(null);
                    swfNextAprvDefEntity.setNextAprvUserId(null);
                    swfNextAprvDefEntity.setNextAprvPostName("");
                    return swfNextAprvDefEntity;
                }
            }else {
                swfNextAprvDefEntity.setNextAprvPostId(null);
                swfNextAprvDefEntity.setNextAprvPostName("");
                return swfNextAprvDefEntity;
            }
        } else {
            return null;
        }
    }

    /**
     * 更新下一审批人默认值
     *
     * @param swfNextAprvDefEntity 下一审批人默认值实体类
     * @return void
     * @Author Dingyayong
     * @Date 2020/4/13 17:59
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateSwfNextAprvDef(SwfNextAprvDefEntity swfNextAprvDefEntity) {
        baseDao.updateSwfNextAprvDef(swfNextAprvDefEntity);
    }

    /**
     * 借款申请获取下一审批人默认值
     *
     * @param userId 用户id
     * @param compId 公司id
     * @return com.softtown.modules.entity.SwfNextAprvDefEntity
     * @author fuming
     * @date 2019/10/29 13:18
     */
    @Override
    public SwfNextAprvDefEntity getAdvNextAprv(Long userId, Long compId) {
        SwfNextAprvDefEntity swfNextAprvDefEntity = baseDao.getAdvNextAprvDef(userId, compId);
        if (swfNextAprvDefEntity != null) {
            CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(compId, swfNextAprvDefEntity.getNextAprvUserId());
            List<CompOrgEmplPosnEntity> compOrgEmplPosnEntityList = compOrgEmplPosnService.findListPosnEntity(compId, compSysUserEmplEntity.getEmplId());
            List<Long> posnIdS = new ArrayList<>();
            for (CompOrgEmplPosnEntity compOrgEmplPosnEntity : compOrgEmplPosnEntityList) {
                posnIdS.add(compOrgEmplPosnEntity.getPosnId());
            }
            if(posnIdS.size() > 0 && swfNextAprvDefEntity.getNextAprvPostId() != null) {
                if (posnIdS.contains(swfNextAprvDefEntity.getNextAprvPostId())) {
                    return swfNextAprvDefEntity;
                } else {
                    swfNextAprvDefEntity.setNextAprvPostId(null);
                    swfNextAprvDefEntity.setNextAprvUserId(null);
                    swfNextAprvDefEntity.setNextAprvPostName("");
                    return swfNextAprvDefEntity;
                }
            }else {
                swfNextAprvDefEntity.setNextAprvPostId(null);
                swfNextAprvDefEntity.setNextAprvPostName("");
                return swfNextAprvDefEntity;
            }
        } else {
            return null;
        }
    }

    /**
     * 根据类型获取下一审批人默认值
     *
     * @param
     * @return
     * @author Dingyayong
     * @date 2019/8/22 17:03
     */
    @Override
    public SwfNextAprvDefEntity getNextAprvtByDocType(Long userId, Long compId, String docType) {
        SwfNextAprvDefEntity swfNextAprvDefEntity = baseDao.getNextAprvtByDocType(userId, compId, docType);
        if (swfNextAprvDefEntity != null) {
            CompSysUserEmplEntity compSysUserEmplEntity = compSysUserEmplService.findCompEmpl(compId, swfNextAprvDefEntity.getNextAprvUserId());
            List<CompOrgEmplPosnEntity> compOrgEmplPosnEntityList = compOrgEmplPosnService.findListPosnEntity(compId, compSysUserEmplEntity.getEmplId());
            List<Long> posnIdS = new ArrayList<>();
            for (CompOrgEmplPosnEntity compOrgEmplPosnEntity : compOrgEmplPosnEntityList) {
                posnIdS.add(compOrgEmplPosnEntity.getPosnId());
            }
            if(posnIdS.size() > 0 && swfNextAprvDefEntity.getNextAprvPostId() != null) {
                if (posnIdS.contains(swfNextAprvDefEntity.getNextAprvPostId())) {
                    return swfNextAprvDefEntity;
                } else {
                    swfNextAprvDefEntity.setNextAprvPostId(null);
                    swfNextAprvDefEntity.setNextAprvUserId(null);
                    swfNextAprvDefEntity.setNextAprvPostName("");
                    return swfNextAprvDefEntity;
                }
            }else {
                swfNextAprvDefEntity.setNextAprvPostId(null);
                swfNextAprvDefEntity.setNextAprvPostName("");
                return swfNextAprvDefEntity;
            }
        } else {
            return null;
        }
    }

    /**
     * 获取下一个审批人的上一审批人段终审值
     *
     * @param swfNextAprvDefDTO 下一审批默认值DTO
     * @return java.lang.Integer
     * @Author fuming
     * @Date 2020/3/4 17:10
     */
    @Override
    public Integer selectIsPhaseLastAprv(SwfNextAprvDefDTO swfNextAprvDefDTO) {
        SwfTaskEntity taskEntity = swfTaskService.getLastswfHstAprv(swfNextAprvDefDTO.getCompId(), swfNextAprvDefDTO.getUserId(), swfNextAprvDefDTO.getProcInstId());
        return baseDao.selectIsPhaseLastAprv(taskEntity.getCreator(), swfNextAprvDefDTO.getCompId(), swfNextAprvDefDTO.getNextAprvUserId(), swfNextAprvDefDTO.getDocTypeValue());
    }
}
